package com.guoxin.chapter3.waitnotify.notifyholdlock;

/**
 * @author guoxin
 * @date 2019年8月22日
 */
public class Service {
	public void testMethod(Object lock){
		try {
			synchronized (lock) {
			System.out.println("begin wait() ThreadName="+Thread.currentThread().getName());
			lock.wait();
			System.out.println("end wait() ThreadName="+Thread.currentThread().getName());
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 必须执行完notify()方法所在的同步synchronized代码块后才释放锁
	 * 
	 * @param lock 监视器锁
	 */
	public void synNotifyMethod(Object lock) {
		try {
			synchronized (lock) {
				System.out.println("begin notify() ThreadName=" + Thread.currentThread().getName() + " time="
						+ System.currentTimeMillis());
				lock.notify();
				Thread.sleep(5000);
				System.out.println("end notify() ThreadName=" + Thread.currentThread().getName() + " time="
						+ System.currentTimeMillis());

			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
